package com.leo.auth.mapper;

import com.leo.auth.entity.Permission;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * 权限表 Mapper 接口
 * </p>
 *
 * @author Leo
 * @since 2021-04-07
 */
@Repository
public interface PermissionMapper extends BaseMapper<Permission> {

    /**
     * 根据用户id查询用户权限列表
     * @param userId 用户id
     * @return List<Permission>
     */
    @Select("SELECT " +
            "p.* " +
            "FROM " +
            "user AS u " +
            "LEFT JOIN user_role_relation AS ur " +
            "ON u.id = ur.user_id " +
            "LEFT JOIN role AS r " +
            "ON r.id = ur.role_id " +
            "LEFT JOIN role_permission_relation AS rp " +
            "ON r.id = rp.role_id " +
            "LEFT JOIN permission AS p " +
            "ON p.id = rp.permission_id " +
            "WHERE u.id = #{userId}")
    List<Permission> selectListByUserId(Integer userId);

    /**
     * 根据接口路径查询该接口对应的权限code数组
     * @param requestUrl 接口路径
     * @return String[]
     */
    @Select("SELECT a.permission_code " +
            "FROM permission AS a " +
            "JOIN request_path_permission_relation AS b " +
            "ON a.id = b.permission_id " +
            "JOIN request_path AS c " +
            "ON b.url_id = c.id " +
            "WHERE c.url = #{requestUrl} ")
    String[] selectListByPath(String requestUrl);
}
